package student_01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class GetAppIcon {

	public static void main(String[] args) {

		urlArr("http://apk.hiapk.com/apps/Tools");
		/*Document dom = Jsoup.parse(html);

		Elements imgs = dom.getElementsByTag("img");

		for (int i = 0; i < imgs.size(); i++) {

			String imgUrl = imgs.get(i).attr("src");
			try {
				download(imgUrl,"ims"+i+".png","D:\\学习\\APP\\素材");
				System.out.println(imgUrl+" 下载成功");
			} catch (Exception e) {
				System.out.println("下载["+imgUrl+"]时出错");
			}
			

		}*/

	}
	
	public static List urlArr(String url){
		
		List list = new ArrayList();
		
		String html = connHtml(url);
		
		Document dom = Jsoup.parse(html);
		
		Elements imgs = dom.getElementsByTag("a");
		
		for (int i = 0; i < imgs.size(); i++) {
			
			String src = imgs.get(i).attr("src");
			list.add(src);
			System.out.println(src);
			
		}
		
		return list;
	}

	/**
	 * 爬去网页源代码
	 * 
	 * @param uri
	 * @return
	 */
	public static String connHtml(String uri) {
		// 获取app图标
		URL url = null;

		try {
			url = new URL(uri);
		} catch (MalformedURLException e) {
			System.out.println("链接异常");
		}

		URLConnection conn = null;

		try {
			conn = url.openConnection();
		} catch (IOException e) {
			System.out.println("网络异常");
		}

		// 设置网络超时时间
		conn.setConnectTimeout(6000);

		InputStream is = null;

		try {
			is = conn.getInputStream();
		} catch (IOException e) {
			System.out.println("无法得到输入流");
		}

		BufferedReader br = new BufferedReader(new InputStreamReader(is));

		String str = null;

		StringBuffer sb = new StringBuffer();

		try {

			while ((str = br.readLine()) != null) {

				sb.append(str + "\n");

			}

		} catch (IOException e) {

			System.out.println("拼接字符串时出错");

		}

		return sb.toString();
	}

	/**
	 * 下载文件到本地
	 * @param urlString
	 * @param filename
	 * @param savePath
	 * @throws Exception
	 */
	public static void download(String urlString, String filename, String savePath) throws Exception {
		// 构造URL
		URL url = new URL(urlString);
		// 打开连接
		URLConnection con = url.openConnection();
		// 设置请求超时为5s
		con.setConnectTimeout(5 * 1000);
		// 输入流
		InputStream is = con.getInputStream();

		// 1K的数据缓冲
		byte[] bs = new byte[1024];
		// 读取到的数据长度
		int len;
		// 输出的文件流
		File sf = new File(savePath);
		if (!sf.exists()) {
			sf.mkdirs();
		}
		OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename);
		// 开始读取
		while ((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		// 完毕，关闭所有链接
		os.close();
		is.close();
	}
}
